#ifndef __SOCKETIO_H__
#define __SOCKETIO_H__
#include "Buffer.h"
#include <iostream>

typedef enum {
    INPUT = 0,
    OUTPUT = 1
}BufferType;

class SocketIO
{
public:
    explicit SocketIO(int fd);
    ~SocketIO()
    {
    }
    size_t readn(char *buf, size_t len);
    size_t readLine(char *buf, size_t len);
    size_t writen(const char *buf, size_t len);

    ssize_t readToBuffer(BufferType type = INPUT);
    ssize_t flushBuffer(BufferType type = OUTPUT);
    string readAllFromBuffer(BufferType type = INPUT)
    {
        return getBuffer(type).retrieveAllAsString();
    }

    ssize_t writeFromBuffer(BufferType type = OUTPUT);
    ssize_t writeToBuffer(const string &msg, BufferType type = OUTPUT) {
        getBuffer(type).append(msg.c_str(), msg.size());
        return msg.size();
    }

private:
    int _fd;

    Buffer _inputBuffer;
    Buffer _outputBuffer;
    Buffer& getBuffer(BufferType type) {
        return (type == INPUT) ? _inputBuffer : _outputBuffer;
    }

};


#endif

